// pages/job_intention/job_intention.js
const app = getApp(), core = require('../../utils/core'), util = require('../../utils/util'), WeValidator = require('../../utils/we-validator');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    types: [{id:1,name:'全职'},{id:2,name:'兼职'}],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getOpenerEventChannel().on('dataTransfer', data => {
      this.setData({
        model: Object.assign({},data.model), //创建一份浅拷贝, 不影响上一页的值
      });
    });
    let city = app.getCurrentCity();
    this.setData({cityName: city.name+'市'});
    util.getCities().then(districts=>{
      this.setData({districts});
    });
    
    util.getJobCategories().then(items=>{
      this.setData({jobCategories: items});
    });
    util.getSalaryOptions(3).then(items=>{
      this.setData({salaryOptions: items});
    });
    util.getWorkStates().then(items=>{
      this.setData({workStates:items});
    });

    util.getIndustryCategories().then(items=>{
      this.setData({industryCategories:items});
    });

  },
  bindPickerChange(event) {
    let name = event.currentTarget.dataset.name, value = event.detail.items[0].id;
    this.setData({['model.'+name]: value});
  },
  bindFavCategoryChange(event) {
    let item = event.detail.items[2];
    this.setData({favCategoryName: item.category_name, 'model.fav_category_id': item.id});
  },
  bindPreviousJobCategoryChange(event) {
    let item = event.detail.items[2];
    this.setData({previousJobCategoryName: item.category_name, 'model.previous_job_category_id': item.id});
  },
  bindFavAreaChange(event) {
    this.setData({
      favAreaName: event.detail.items.map(item=>item.name).join(','),
      'model.fav_province_id': event.detail.items[0].id,
      'model.fav_city_id': event.detail.items[1].id,
    })
  },

  bindSubmit(event) {
    let data = event.detail.value;
    data.module = 'job_intention';
    core.post({
      url: 'xilujob.user/save_module',
      data: data,
      success: ret => {
        this.getOpenerEventChannel().emit('dataTransfer', {model: ret.data.model, complete_percent:ret.data.complete_percent});
        wx.navigateBack();
      }
    });
  }

});
